package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IGlobalSearchService;
import yl.hs.bmipfull.viewmodels.OptionModel;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import java.util.Arrays;

/*
 * GlobalSearchController
 * @category
 * @author 谢景广
 * @date 2021-06-29
 */
@Api(value = "全文搜索", tags = "系统管理——全文搜索")
@RequestMapping("/api/globalSearch")
@RestController
public class GlobalSearchController extends ControllerBase {
    @Autowired
    IGlobalSearchService itemService;
    @ApiOperation(value = "查询搜索设置项", notes = "查询搜索设置项")
    @RequestMapping(value = "itemsPage/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPageItems(
            @RequestBody PageQuery pageQuery,
            @PathVariable int rowNum,
            @PathVariable int page) {

        return success("ok",
                itemService.queryListByPage(pageQuery, page, rowNum));
    }
    @ApiOperation(value = "获取搜索设置项选择项", notes = "获取搜索设置项选择项")
    @RequestMapping(value = "selectItems",
            method = RequestMethod.GET)
    public WebResult querySelectItems() {
        var list = itemService.queryAllItems();
        return success("ok",
                list.stream().map(t ->
                        new OptionModel(t.getDataCode(), t.getDataType())
                ).toList());
    }

    @ApiOperation(value = "搜索数据", notes = "搜索数据")
    @RequestMapping(value = {"search/{key}/{dataCode}", "search/{key}"},
            method = RequestMethod.GET)
    public WebResult search(@PathVariable String key,
                            @PathVariable(required = false) String dataCode) {

        return success("ok",
                itemService.search(key, dataCode));
    }
    @ApiOperation(value = "获取字段的数据库值", notes = "获取字段的数据库值")
    @RequestMapping(value = {"fieldValue/{tableName}/{field}"},
            method = RequestMethod.GET)
    public WebResult tableFieldValueGroupField(@PathVariable String tableName,
                            @PathVariable String field) {
        return success("ok", itemService.tableFieldValueGroupField(tableName, field));
    }

    @ApiOperation(value = "保存搜索设置项",
            notes = "保存搜索设置项")
    @RequestMapping(value = "saveItems",
            method = RequestMethod.POST)
    public WebResult saveSearchItem(@RequestBody SubmitModel model) {
        Integer result = null;
        try {
            result = itemService.saveSearchItem(model);
        } catch (Exception e) {
            return error(e);
        }
        return success("保存成功", result);
    }

    @ApiOperation(
            value = "判断数据代码搜索设置项是否存在",
            notes = "判断数据代码搜索设置项是否存在")
    @RequestMapping(
            value = {"checkExists/{dataCode}/{oldSN}", "checkExists/{dataCode}"},
            method = RequestMethod.GET)
    public WebResult checkDataCode(
            @PathVariable String dataCode, @PathVariable(required = false) String oldSN) {
        var result = itemService.checkDataCode(dataCode, oldSN);
        return success("保存成功", result);
    }

    @ApiOperation(value = "删除搜索设置项", notes = "删除搜索设置项")
    @RequestMapping(value = "deleteItem/{sn}", method = RequestMethod.DELETE)
    public WebResult deleteItems(@PathVariable String sn) {
        var result = itemService.
                deleteDataBySNList(
                        Arrays.stream(sn.split(",")).toList());
        return success("删除完成", result);
    }
}
